<?php
include_once("./lib/redis.class.php");
include_once("./lib/logger.php");
include_once("./connConfig.php");
include_once("./lib/httpclient.php");
error_reporting(0);
date_default_timezone_set('Asia/Shanghai');

function consume(){
	$redis = new Redis();
	$redis_config = Config::$redis_data_config;
	$redis_name = 'redis0';
    $ret = $redis->connect($redis_config[$redis_name]['host'],$redis_config[$redis_name]['port']);
    if (!$ret) {
		Config::$loggerDebug->debug('customer','redis connect error!');
		exit(0);
    }
    $ret = $redis->auth($redis_config[$redis_name]['pass']);
    if (!$ret) {
		Config::$loggerDebug->debug('customer','redis auth error!');
		exit(0);
	}

	$report_config = Config::$info_report;
	$token = '';
	$http = new HttpClient();
	
	//获取上报token
	if($redis->exists('report_token')){
		 $token = $redis->hget('report_token','token');
	}else{
		$url = 'http://120.77.51.159:5050/logapi/loguser/login/';		
		$req_data = array('data'=>array('cid'=>$report_config['cid'],'username'=>$report_config['user'],'passwd'=>$report_config['pass']));
		Config::$loggerDebug->debug('customer','request token: '.json_encode($req_data));
		$result = $http->AsyncPostjson($url,$req_data);
		Config::$loggerDebug->debug('customer','response token: '.$result);
		$result = json_decode($result,true);
		if ($result['code'] == 200){
			$token = $result['info'];
			$redis->hset('report_token','token',$token);
			$redis->expire('report_token',$report_config['reportTokenTime']);
		}else{
			Config::$loggerDebug->debug('customer','获取token fail');
			exit(0);
		}
	}
		
	$redis->select(3);
	//获取队列长度
	$len = $redis->Llen('pay_data');
	if ($len > 0){
		$value = $redis->lpop('pay_data');
		//上报支付数据
		$url = 'http://120.77.51.159:5050/logapi/pay/';
		$valueArr = json_decode($value,true);
		$data = array('data'=>array($valueArr),'cid'=>$report_config['cid'],'secret_key'=>$token);
		$res = $http->AsyncPostjson($url,$data);
		Config::$loggerDebug->debug('customer','response pay: '.$res);		
	}
	
	$redis->close();
}
consume();

?>